<div class="box-header">
	<h3>GIỎ HÀNG CỦA BẠN</h3>
</div>
<?php 
	if(!empty($this->arrCart)):
		$yourCart = new Zend_Session_Namespace('cart');
		$carInfo = $yourCart->getIterator();
	
		$config = new Zendvn_Controller_Config(array('module'=>'admin',
												'fileConfig'=>'product-category.ini'));
		$itemConfig = $config->getconfig('item');
		
		$tblItem = new Zendvn_Model_Table_Product();
		
		$comment = $this->formTextarea('comment', $carInfo['comment']);
	
?>
<div class="my_cart">
	<div class="lev header">
		<div class="image">Hình</div>
		<div class="name">Tên sản phẩm</div>
		<div class="price">Giá bán (1 SP)</div>
		<div class="quantity">Số lượng đặt mua</div>
		<div class="total_price">Thành tiền</div>
	</div>
	<?php 
		$totalPrice = 0;
		foreach ($this->arrCart as $k => $v):
			$productInfo 	= $tblItem->getItem(array('id' => $v['id']));
			
			$picture 		= $productInfo['image'];
			
			if(file_exists($itemConfig['uploadDir'] . '/' . $picture) && !empty($picture)){
				$picture = $itemConfig['uploadUrl'] . '/' . $picture;
			}else{
				$picture = FILE_URL . '/no-image.png';
			}
			
			$name			= Zendvn_Filter_Filter::filterContent($productInfo['name'], 'filter');
			$size			= $this->slbSize[$v['size']];
			$size			= Zendvn_Filter_Filter::filterContent($size, 'filter');
			$color			= $this->slbColor[$v['color']];
			$color			= Zendvn_Filter_Filter::filterContent($color, 'filter');
			$price			= $v['price'];
			$quantity		= $v['quantity'];
			
			$pricePerItem	= $price * $quantity;
			$totalPrice		= $totalPrice + $pricePerItem;
		
	?>
	<div class="lev item" id="cartItem-<?php echo $k?>">
		<div class="image">
			<a href="<?php echo $picture?>" onclick="return fancyBox(this)">
				<img src="<?php echo $picture?>" />
			</a>
		</div>
		<div class="name">
			<p><?php echo $name?></p>
			<p class="size">Size: <?php echo $size?></p>
			<p class="color">Color: <?php echo $color?></p>
		</div>
		<div class="price"><?php echo number_format($price)?></div>
		<div class="quantity">
			<span rel="<?php echo $k?>" class="decrease tipTip" title="Giảm 1"></span> 
			<span class="number" id="quantity-<?php echo $k?>"><?php echo $quantity?></span> 
			<span rel="<?php echo $k?>" class="increase tipTip" title="Tăng 1"></span>
		</div>
		<div class="total_price">
			<span id="totalPrice-<?php echo $k?>"><?php echo number_format($pricePerItem)?></span> 
			<span rel="<?php echo $k?>" class="delete tipTip" title="Xoá sản phẩm này"></span>
		</div>
	</div>
	<?php endforeach;?>
	<div class="footer">
		<div class="cart_left">Tổng cộng</div>
		<div class="cart_right" id="totalPrice"><?php echo number_format($totalPrice)?></div>
	</div>
	<div class="footer">
		<div class="cart_left">Khuyến mãi</div>
		<div class="cart_right">0</div>
	</div>
	<div class="footer">
		<div class="cart_left">Tính tổng</div>
		<div class="cart_right" id="endPrice"><?php echo number_format($totalPrice)?></div>
		<input type="hidden" id="hidden_end_price" name="hidden_end_price" value="<?php echo $totalPrice?>" />
	</div>
</div>
<div class="cart_comment">
	<p>Ghi chú về đơn hàng</p>
	<?php echo $comment?>
</div>
<div class="cart_button">
	<a href="#"><span>Thực hiện thanh toán</span></a>
</div>
<?php 
	else:
		echo '<div class="warning_empty" style="background-position-x: 150px;">Hiện tại chưa có sản phẩm nào trong giỏ hàng của bạn.</div>';
	endif;
?>
<?php 
	$linkProcessCart = $this->cmsUrl(array(
										'module' => 'default',
										'controller' => 'index',
										'action' => 'process-cart'
										));
	$linkCheckout = $this->cmsUrl(array(
										'module' => 'default',
										'controller' => 'index',
										'action' => 'check-out'
										));									
?>
<script>
	$(document).ready(function(){
		$('.decrease').click(function(){
			var key = $(this).attr('rel');
			var totalPrice = $('#hidden_end_price').val();
			$.post('<?php echo $linkProcessCart?>' + '/totalPrice/' + totalPrice + '/type/decrease/key/' + key, function(data){
				data = $.parseJSON(data);
				if(data.type == 'minimum'){
					jAlert('Bạn không thể giảm số lượng được nữa', 'Thông báo');
				}else{
					$('#quantity-' + key).text(data.quantity);
					$('#totalPrice-' + key).text(data.totalPriceItem);
					$('#endPrice, #totalPrice').text(data.totalPrice);
					$('#hidden_end_price').val(data.hiddenTotal);
				}
			})
		})

		$('.increase').click(function(){
			var key = $(this).attr('rel');
			var totalPrice = $('#hidden_end_price').val();
			$.post('<?php echo $linkProcessCart?>' + '/totalPrice/' + totalPrice + '/type/increase/key/' + key, function(data){
				data = $.parseJSON(data);
				if(data.type == 'maximum'){
					jAlert('Không còn sản phẩm nào trong kho', 'Thông báo');
				}else{
					$('#quantity-' + key).text(data.quantity);
					$('#totalPrice-' + key).text(data.totalPriceItem);
					$('#endPrice, #totalPrice').text(data.totalPrice);
					$('#hidden_end_price').val(data.hiddenTotal);
				}
			})
		})

		$('.delete').click(function(){
			var key = $(this).attr('rel');
			var totalPrice = $('#hidden_end_price').val();
			$.post('<?php echo $linkProcessCart?>' + '/totalPrice/' + totalPrice + '/type/delete/key/' + key, function(data){
				data = $.parseJSON(data);
				$('#cartItem-' + key).fadeOut();
				$('#endPrice, #totalPrice').text(data.totalPrice);
				$('#hidden_end_price').val(data.hiddenTotal);
			})
		})

		$('.cart_button a').click(function(){
			var comment = $('.cart_comment textarea').val();
			$.post('<?php echo $linkProcessCart?>' +'/type/submit', {comment: comment}, function(data){
				window.location.href = '<?php echo $linkCheckout?>';
			})
			return false;
		})
	})
</script>
